package icu.lyt0628;

import io.netty.buffer.ByteBuf;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.util.GenericOptionsParser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
import java.nio.ByteBuffer;
import java.util.Arrays;

/**
 * Hello world!
 *
 */
public class App 
{
    public Configuration conf = null;
    private FileSystem fs = null;


    public static Logger LOG = LoggerFactory.getLogger("App");
    public App(){
        conf = new Configuration(true);
        conf.set("hadoop.home.dir", "F:\\wsl_ubuntu\\hadoop-3.3.6");
    }


    public void connect(){

        try {
            fs = FileSystem.get(URI.create("hdfs://172.21.0.2:9000"), conf, "hadoop");
        } catch (IOException | InterruptedException e) {
            throw new RuntimeException(e);
        }

    }


    public void release() {
        try {
            fs.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public void mkdir(String path){
        try {
            fs.mkdirs(new Path(path));

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public void upload(String lcl, String dst){
        try {
            fs.copyFromLocalFile(new Path(lcl), new Path(dst));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public boolean exists(String path){
        try {
           return fs.exists(new Path(path));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

    public void download(String src, String dst){
        try {
            fs.copyToLocalFile(new Path(src), new Path(dst));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public String cat(String path) {
        try {
            FSDataInputStream open = fs.open(new Path(path));
            StringBuilder sb = new StringBuilder();
            ByteBuffer bf = ByteBuffer.allocate(1024);

            while (open.read(bf) > 0) {
                sb.append(new String(bf.array(), 0, bf.array().length));
            }

            return sb.toString();

        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

    public String stat (String file){
        try {
         return  fs.getFileStatus(new Path(file)).toString();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public void ls(String path){
        try {
            RemoteIterator<LocatedFileStatus> itor = fs.listFiles(new Path(path), true);
            while (itor.hasNext()){
                LocatedFileStatus status = itor.next();
                System.out.println(status.toString());
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public void touch(String file){
        try {
            fs.createNewFile(new Path(file));

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public void rm(String file){
        try {
            fs.delete(new Path(file), true);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public boolean isEmpty(String path){
        try {
            RemoteIterator<LocatedFileStatus> itor = fs.listFiles(new Path(path), true);
            return ! itor.hasNext();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public void rmDir(String path){
        rm(path);
    }

    public void append(String file, String content ,boolean tail){
        String ori = "";
        if (exists(file)){
            ori = cat(file);
        }
        String r;
        if (tail){
            r =ori + content;
        }else {
            r = content + ori;
        }
        rm(file);
        touch(file);
        try {
            FSDataOutputStream os = fs.append(new Path(file));
            os.write(r.getBytes());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
    public void append(String file, String content){
        append(file, content,true);
    }

    public void move(String src, String dst){
        if (!exists(src)) {
            return;
        }
        String s = cat(src);
        try {
            fs.create(new Path(dst));
            append(dst, s);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            rm(src);
        }
    }


    public static void main( String[] args )
    {
        App app = new App();

        Configuration cfg = app.conf;
        try {
            String[] restParams = new GenericOptionsParser( cfg, args).getRemainingArgs();
            System.out.println(Arrays.toString(restParams));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }



    }
}
